package com.csii.mobilebanking.funbox;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.xmlpull.v1.XmlPullParserException;

import com.csii.mobilebanking.R;
import com.csii.mobilebanking.funbox.jar.PluginBuilder;
import com.csii.mobilebanking.funbox.jar.PluginDescription;
import com.csii.mobilebanking.funbox.jar.PluginInvoke;
import com.csii.mobilebanking.funbox.jar.PluginSearch;
import com.csii.mobilebanking.funbox.jar.beans.Plugin;
import com.csii.mobilebanking.funbox.jar.beans.PluginFeature;
import com.csii.mobilebanking.funbox.jar.beans.PluginIntent;
import com.csii.mobilebanking.user.HelpExpListAc;

import android.app.Activity;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

public class FunboxAc extends Activity {
	private LinearLayout llPluginList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.funbox);
		initView();
		try {
			findMyPlugin();
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
	}

	private void initAddedBoxes() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < list.size(); i++) {

			PluginItemIndexView itemV = new PluginItemIndexView(this);
			itemV.setDes(list.get(i).get("des").toString());
			Drawable d = getResources().getDrawable(R.drawable.ic_launcher);// 默认灰色图标
			itemV.setLogoImage(d);

			llPluginList.addView(itemV);
		}

	}

	private void initView() {
		llPluginList = (LinearLayout) findViewById(R.id.main_llPluginList);

	}

	/**
	 * 这里程序查找扩展
	 * 
	 * @throws NameNotFoundException
	 * @throws XmlPullParserException
	 * @throws IOException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 */
	private void findMyPlugin() throws NameNotFoundException, IOException,
			XmlPullParserException, ClassNotFoundException,
			IllegalAccessException, InstantiationException,
			IllegalArgumentException, SecurityException, NoSuchFieldException {

		// 首先，就是查找插件
		PluginSearch psearch = new PluginSearch();
		List<Plugin> plugins = psearch.getPlugins(this);

		// 然后将插件再组装一下
		PluginBuilder pbuilder = new PluginBuilder(this);
		plugins = pbuilder.buildPluginsDescrition(plugins);

		// 显示出所有插件
		flateUI(plugins);
	}

	/**
	 * 将所有插件列出来
	 * 
	 * @param plugins
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 * @throws NameNotFoundException
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 */
	private void flateUI(List<Plugin> plugins) throws ClassNotFoundException,
			IllegalAccessException, InstantiationException,
			NameNotFoundException, IllegalArgumentException, SecurityException,
			NoSuchFieldException {

		for (final Plugin plug : plugins) {
			// 加入插件
			PluginItem item = new PluginItem(this);

			// 获取插件的描述对象
			PluginDescription<MainDescript> pdes = new PluginDescription<MainDescript>(
					MainDescript.class);

			MainDescript des = pdes.getDescription(this, plug);

			// 设置插件item的描述信息
			item.setDes(des.getDescription());
			// 插件的状态
			int status = plug.getStatus();

			Drawable d = plug.getContext().getResources()
					.getDrawable(des.getIconResId());
			item.setLogoImage(d);
			// item.setBackgroundResource(des.getIconResId());//无style效果
			// 获得当前插件的功能
			List<PluginFeature> features = plug.getFeatures();

			// 遍历intent -只有一个啊？
			Map<String, PluginIntent> intents = plug.getIntents();

			Iterator<Map.Entry<String, PluginIntent>> iterator = intents
					.entrySet().iterator();

			while (iterator.hasNext()) {
				final Map.Entry<String, PluginIntent> entry = iterator.next();
				item.addPluginMethod(entry.getValue(), new OnClickListener() {
					@Override
					public void onClick(View v) {
						PluginInvoke pi = new PluginInvoke(FunboxAc.this);
						pi.invoke(entry.getValue());
					}
				});

			}
			// 将插件加入到ui
			llPluginList.addView(item);
		}
		
		initAddedBoxes();
	}

}
